package com.hns.tool.pub;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
 * object转化为map
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class DataConvertUtil {
    
	/**
	 * 反射将object转化为map，属性名字符串作为key，属性值为value
	 * @param obj Object数据
	 * @return 转化后的map数据
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
    @SuppressWarnings("rawtypes")
    public static Map ConvertObjToMap(Object obj){
        Map<String,Object> reMap = new HashMap<String,Object>();
        if (obj == null) 
            return null;
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            for(int i=0;i<fields.length;i++){
                try {
                    Field f = obj.getClass().getDeclaredField(fields[i].getName());
                    f.setAccessible(true);
                    Object o = f.get(obj);
                    reMap.put(fields[i].getName(), o);
                } catch (NoSuchFieldException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return reMap;
    }
}    